<?php

namespace App\Jobs;

use App\Models\SeckillOrderModel;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use think\facade\Log;

class NoPayOrderJob implements ShouldQueue
{
	use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
	
	/**
	 * Create a new job instance.
	 *
	 * @return void
	 */
	public $order_number;
	public $redis;
	public $seckillId;
	
	public function __construct($order_number = '', $seckillId = '')
	{
		//
		$this->order_number = $order_number;
		$this->seckillId = $seckillId;
		$this->redis = new \Redis();
		$this->redis->connect('127.0.0.1', 6379);
	}
	
	/**
	 * Execute the job.k
	 *
	 * @return void
	 */
	public function handle()
	{
		//订单号 查 支付状态
		$payState = (new SeckillOrderModel())->where('order_number', $this->order_number)->pluck('pay_state')->first();
		if ($payState == 0) {
			//修改支付状态为过期
			(new SeckillOrderModel())->where('order_number', $this->order_number)->update(['pay_state' => 2]);
			//把抢的1个数量回到队列
			$this->redis->lPush('number_' . $this->seckillId, 1);
			return;
		}
		if ($payState == 1) {
			Log::info($this->order_number . '支付成功');
		}
	}
}
